﻿namespace Jhong.FunMVCBlog.Admin.Controllers
{
    using Jhong.FunMVC;
    using Jhong.FunMVCBlog.Core.Data;
    using Jhong.FunMVCBlog.Core.Entity;
    using Jhong.FunMVCBlog.Core.Infrastructure;

    public class LinkController : SecurityController
    {
        private LinksData _linkData = new LinksData();

        public ActionResult List()
        {
            return View();
        }

        public ActionResult GetData(int pageIndex, int itemsPerPage)
        {
            var data = this._linkData.GetList(pageIndex, itemsPerPage);
            var page = data.GetPageHelper();
            return AjaxResult(true, data: new { Items = data.Items, Page = page.GetPageHtml() });
        }

        public ActionResult EditLink(string id)
        {
            if (string.IsNullOrWhiteSpace(id)) ViewData["model"] = new Links();
            else ViewData["model"] = this._linkData.GetByID(int.Parse(id));
            return View();
        }

        [HttpPost]
        public ActionResult EditLink(Links model)
        {
            bool res = false;
            if (model.ID == 0)
                res = this._linkData.Insert(model);
            else
                res = this._linkData.Update(model);
            return AjaxResult(res, res ? "保存成功" : "保存失败");
        }

        public ActionResult DeleteLink(int id)
        {
            var res = this._linkData.Delete(new Links() { ID = id });
            return AjaxResult(res, res ? "删除成功" : "删除失败");
        }
    }
}